<template>
  <div id="container" v-if="flag"></div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader'
import { getMap } from '@/api/map'
import { getAreaHouse } from '@/api/house'

export default {
  name: '',
  props: {
    body: {
      type: Array,
      default: () => []
    }
  },
  components: {},
  data () {
    return {
      map: null,
      location: '',
      renderList: [],
      flag: false,
    }
  },
  methods: {
    async initMap () {
      const AMap = await AMapLoader.load({
        key: 'c2d58d1a48dc656eb931da16f8fdf422', // 申请好的Web端开发者Key，首次调用 load 时必填
        version: '2.0', // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ['AMap.PlaceSearch', 'AMap.AutoComplete', 'AMap.Geocoder'] // 需要使用的的插件列表，如比例尺'AMap.Scale'等
      })
  
      this.map = new AMap.Map('container', {
        //设置地图容器id
        // viewMode:"3D",    //是否为3D地图模式
        zoom: 10, //初始化地图级别
        center: this.location //初始化地图中心点位置
      })
      this.renderList.forEach(item => {
        const x = item.coord.latitude
        const y = item.coord.longitude
        const arr = []
        arr.push(y)
        arr.push(x)
        const content = `<div class="marker-route" onclick="mapClick">
      <p>${item.label}</p>
     <span>${item.count}</span>
          </div>`
        const marker = new AMap.Marker({
          content: content, // 自定义点标记覆盖物内容
          position: arr, // 基点位置
          offset: new AMap.Pixel(-17, -42) // 相对于基点的偏移位置
        })
        marker.on('click',async function(){
          this.flag=false
          try {
          const {data:{body}}= await getAreaHouse({id:item.value})
         this.location=arr
        this.renderList=body
        console.log(this.renderList);
        console.log(this.location);
        this.initMap()
       this.flag=true
          } catch (error) {
            console.log(error);
          }

        })
        this.map.add(marker)
      })
      

    },
  //   mapClick(value,a){
  //  console.log(value);
  //  console.log(a);
  //     }
  },

  async created () {
    const city = localStorage.getItem('text')
    const data = await getMap(city)
    // console.log(data.geocodes[0].location.split(','))
    this.location = data.geocodes[0].location.split(',') || [
      '121.473701,31.230416'
    ]
    // console.log(body);
    this.renderList = this.body
    this.flag = true
  },

  mounted () {
    this.initMap()
  },

}
</script>

<style scoped lang="less">
#container {
  padding: 0;
  margin: 0;
  width: 100%;
  height: 1000px;
}
/deep/.marker-route {
  display: flex;
  flex-direction: column;
  width: 70px;
  height: 70px;
  border-radius: 50%;
  font-size: 12px;
  background-color: rgba(12, 181, 106, 0.9);
  text-align: center;
  color: #fff;
  justify-content: center;
  p {
    margin-bottom: 5px;
  }
}
</style>
